package g2.routemaster.gui.wizards;

import g2.routemaster.gui.GManager;
import g2.routemaster.gui.general.ECityCard;
import g2.routemaster.gui.general.ImageRegistry;
import g2.routemaster.model.CityCard;
import g2.routemaster.model.GameManager;
import g2.routemaster.model.Hand;
import g2.routemaster.model.PlayedStack;
import g2.routemaster.model.Player;

import java.util.Iterator;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

public class ShowPlayerCardsPage extends WizardPage {

   Group boardCardsGroup;
   Group handCardsGroup;
   Button bPlayFirst;
   Button bPlayLast;

   int numColumns = 7;

   protected ShowPlayerCardsPage() {
      super("playCardPage");
      setTitle("Cards");
      setDescription("Player cards");
   }

   public void createControl(Composite parent) {
      Composite composite = new Composite(parent, SWT.NONE);
      composite.setLayout(new GridLayout());

      boardCardsGroup = new Group(composite, SWT.NONE);
      boardCardsGroup.setText("Cards on Board");
      boardCardsGroup.setLayout(new GridLayout());
      boardCardsGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL|GridData.HORIZONTAL_ALIGN_CENTER));

      updateBoardCards();

      if(isActivePlayer()){
         handCardsGroup = new Group(composite, SWT.NONE);
         handCardsGroup.setText("Cards on Hand");
         GridLayout layout = new GridLayout();
         layout.numColumns = numColumns;
         handCardsGroup.setLayout(layout);
         handCardsGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

         updateHandCards();
      }

      // set the composite as the control for this page
      setControl(composite);
      setPageComplete(false);
   }

   void updateBoardCards(){
      Composite cardsComp = new Composite(boardCardsGroup, SWT.NONE);
      GridLayout layout = new GridLayout();
      layout.numColumns = numColumns;
      cardsComp.setLayout(layout);
      cardsComp.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));

      Player player = getPlayer();
      PlayedStack playedStack = player.getPlayedStack();

      if(playedStack.sizeOfCityCard() > 0){
         Iterator iter = playedStack.iteratorOfCityCard();
         while(iter.hasNext()){
            CityCard card = (CityCard)iter.next();
            final CLabel cardPlace = new CLabel(cardsComp, SWT.PUSH);
            cardPlace.setImage(getCardImage(card));
            cardPlace.setToolTipText(card.getCity().getName());
         }
      } else {
         final CLabel cardPlace = new CLabel(cardsComp, SWT.PUSH);
         cardPlace.setImage(getCardImage(null));
         cardPlace.setVisible(false);
      }

      boardCardsGroup.layout();
   }

   void updateHandCards(){
      Player player = getPlayer();
      Hand hand = player.getHand();
      if(hand.sizeOfCityCard() > 0){
         Iterator iter = hand.iteratorOfCityCard();
         while(iter.hasNext()){
            CityCard card = (CityCard)iter.next();
            final CLabel cardPlace = new CLabel(handCardsGroup, SWT.PUSH);
            cardPlace.setData(card);
            cardPlace.setImage(getCardImage(card));
            cardPlace.setToolTipText(card.getCity().getName());
         }
      } else {
         CLabel cardPlace = new CLabel(handCardsGroup, SWT.PUSH);
         cardPlace.setImage(getCardImage(null));
         cardPlace.setVisible(false);
      }
   }

   Image getCardImage(CityCard card){
      ECityCard city = ECityCard.UNKNOWN; 
      if(card != null){
         city = ECityCard.byNom(card.getCity().getName());
      } 
      return ImageRegistry.getCity(city);
   }

   Player getPlayer(){
      ShowPlayerCardsWizard wizard = (ShowPlayerCardsWizard)getWizard();
      return wizard.player;
   }
   
   boolean isActivePlayer(){
      GameManager gm = GManager.instance().getGameManager();
      Player activePlayer = gm.getTurnManager().getPlayer();
      return getPlayer().equals(activePlayer);
   }
}
